﻿using ExitGames.Threading;
using System;
using System.Collections.Generic;
using System.Text;
using Photon.SocketServer;

namespace MOBAServer.Room
{
    /// <summary>
    /// 房间的基类
    /// </summary>
    public class RoomBase<TClient> where TClient:ClientPeer
    {
        /// <summary>
        /// 房间Id
        /// </summary>
        public int id;
        /// <summary>
        /// 连接对象的集合
        /// </summary>
        public List<TClient> clientList;
        /// <summary>
        /// 房间的容纳量
        /// </summary>
        public int roomCount;
        /// <summary>
        /// 定时器
        /// </summary>
        public Timer timer;
        /// <summary>
        /// 定时任务的ID
        /// </summary>
        public Guid GUID;
        public RoomBase(int id,int count)
        {
            this.id = id;
            this.roomCount = count;
            clientList = new List<TClient>();
            GUID = new Guid();
            timer = new Timer();
            timer.Start();
        }
        /// <summary>
        /// 进入房间
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        protected bool EnterRoom(TClient client)
        {
            if(clientList.Contains(client))
            {
                return false;
            }
            else
            {
                clientList.Add(client);
                return true;
            }
        }
        /// <summary>
        /// 离开房间
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        protected bool LeaveRoom(TClient client)
        {
            if (clientList.Contains(client))
            {
                clientList.Remove(client);
                return true;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 开启定时任务
        /// </summary>
        /// <param name="utcTime"></param>
        /// <param name="callback"></param>
        public void StartSchedule(DateTime utcTime,Action callback)
        {
            this.GUID=timer.AddAction(utcTime, callback);
        }
        /// <summary>
        /// 广播
        /// </summary>
        /// <param name="client">收响应的客户端</param>
        /// <param name="opCode">操作码</param>
        /// <param name="subCode">子操作</param>
        /// <param name="parameters">参数</param>
        public virtual void Broast(byte opCode, byte subCode, short returnCode, string debugMessage,TClient exClient = null,params object[] parameters)
        {
            OperationResponse response = new OperationResponse();
            response.OperationCode = opCode;
            response.Parameters = new Dictionary<byte, object>();
            response[80] = subCode;
            for (byte i = 0; i < parameters.Length; i++)
            {
                response[i] = parameters[i];
            }
            response.ReturnCode = returnCode;
            response.DebugMessage = debugMessage;
            foreach (var item in clientList)
            {
                if (item == exClient)
                    continue;
                item.SendOperationResponse(response, new SendParameters());
            }
        }
    }
}
